feat: add Time type support to date_trunc function#19640
feat: add Time type support to date_trunc function#19640Jefffrey merged 6 commits intoapache:mainfrom
Conversation
Co-authored-by: Jeffrey Vo <jeffrey.vo.australia@gmail.com>
| _ => { | ||
| return exec_err!( | ||
| "second argument of `date_trunc` must be timestamp scalar or array" | ||
| "second argument of `date_trunc` must be timestamp, time scalar or array" |
There was a problem hiding this comment.
| "second argument of `date_trunc` must be timestamp, time scalar or array" | |
| "second argument of `date_trunc` must be a timestamp/time scalar or an array" |
| datafusion public date_trunc datafusion public date_trunc FUNCTION true Timestamp(Second, None) SCALAR Truncates a timestamp value to a specified precision. date_trunc(precision, expression) | ||
| datafusion public date_trunc datafusion public date_trunc FUNCTION true Timestamp(Second, Some("+TZ")) SCALAR Truncates a timestamp value to a specified precision. date_trunc(precision, expression) | ||
| datafusion public date_trunc datafusion public date_trunc FUNCTION true Date SCALAR Truncates a timestamp or time value to a specified precision. date_trunc(precision, expression) | ||
| datafusion public date_trunc datafusion public date_trunc FUNCTION true String SCALAR Truncates a timestamp or time value to a specified precision. date_trunc(precision, expression) |
There was a problem hiding this comment.
Are those return types correct - Date and String ? Shouldn't they be converted to Timestamp(Nanosecond, None)
I think the reason is here - https://github.com/apache/datafusion/pull/19640/changes#diff-7a6e17bfd876e9e88341d68750224b8a2294d9f401eef2126a49ade1665c336cR228
There was a problem hiding this comment.
Are these return types or input types?
There was a problem hiding this comment.
SELECT arrow_typeof(date_trunc('day', to_date('2024-01-15')));
-- Returns: Timestamp(ns)
SELECT arrow_typeof(date_trunc('day', '2024-01-15'));
-- Returns: Timestamp(ns)
Yes, the actual runtime return type is Timestamp(Nanosecond), not Date or String.
The coercion from Date / String to Timestamp happens at planning time, before invoke_with_args is called.
The Date / String entries in information_schema represent the signature variants generated by the coercible API’s implicit coercion sources. However, the return_type method receives the already-coerced type (Timestamp) and returns that.
So the functional behavior is correct, this is how coercible signatures are represented in metadata.
|
Thanks @kumarUjjawal & @martin-g |
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> - Part of apache#19025. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? - Added Time64/Time32 signatures to date_trunc - Added time truncation logic (hour, minute, second, millisecond, microsecond) - Error for invalid granularities (day, week, month, quarter, year) <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? Yes <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> --------- Co-authored-by: Jeffrey Vo <jeffrey.vo.australia@gmail.com>
Which issue does this PR close?
Rationale for this change
What changes are included in this PR?
Are these changes tested?
Yes
Are there any user-facing changes?